package leetcode.D400.T309;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-22 16:14:54 星期二
 */

class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length == 1)
            return 0;
        int dp0 = 0, dp1 = Math.max(-prices[0], -prices[1]), dp2=prices[1]-prices[0];
        for(int i=2; i<prices.length; ++i) {
            int tempDp0 = dp0, tempDp1 = dp1, tempDp2 = dp2;
            dp0 = Math.max(tempDp0, tempDp2);
            dp1 = Math.max(tempDp0-prices[i], tempDp1);
            dp2 = tempDp1 + prices[i];
        }
        return Math.max(dp0, dp2);
    }
}